package myJs.detailInfo

import japgolly.scalajs.react.ScalaFnComponent
import japgolly.scalajs.react.vdom.all._
import myJs.api.Api
import myJs.myPkg.Hooks._
import japgolly.scalajs.react.util.EffectCatsEffect._
import myJs.Implicits._
import myJs.tool.Tool

/** Created by yz on 28/11/2023
  */
object BioSampleCp {

  case class Props(id: String)

  val Component = ScalaFnComponent[Props] { props =>
    val (basicData, setBasicData) = useState(ujson.Obj())

    object FTool {

      def refreshBasicData = {
        Api.browse.getBioSample(props.id).flatMap { response =>
          val attributes = response.attributes
          setBasicData(attributes)
        }
      }

    }

    useEffect(
      {
        FTool.refreshBasicData
      },
      List(props.id)
    )

    div(
      h4("Sample Info"),
      div(
        className := "table-responsive",
        marginTop := 20.px,
        table(
          id := "table",
          className := "table table-striped table-hover table-bordered",
          tableLayout.fixed,
          wordWrap.breakWord,
          tbody(
            basicData.value.toList.toVdomArray { case (header, uV) =>
              val v = uV.str
              val trueV = if (v.isDouble) {
                if (v.isInt) {
                  v
                } else {
                  v.toDouble.toFixed(Tool.decDigits)
                }
              } else v
              val inValidNames = List(
                "Not collected",
                "Not applicable",
                "NA",
                "missing"
              )
              val finalV = header match {
                case "geographic location" =>
                  if (inValidNames.containsIgnoreCase(trueV)) {
                    span(trueV)
                  } else {
                    a(
                      target.blank,
                      href := s"https://www.ncbi.nlm.nih.gov/biosample?term=%22geo_loc_name=${trueV}%22[attr]",
                      trueV
                    )
                  }
                case "latitude and longitude" =>
                  if (inValidNames.containsIgnoreCase(trueV)) {
                    span(trueV)
                  } else {
                    a(
                      target.blank,
                      href := s"https://www.google.com/maps/place/${trueV}",
                      trueV
                    )
                  }
                case "culture collection" =>
                  if (inValidNames.containsIgnoreCase(trueV)) {
                    span(trueV)
                  } else {
                    val id = trueV.split(":").last
                    a(
                      target.blank,
                      href := s"http://www.atcc.org/Products/All/${id}.aspx",
                      trueV
                    )
                  }
                case "host taxonomy ID" =>
                  if (inValidNames.containsIgnoreCase(trueV)) {
                    span(trueV)
                  } else {
                    val hostOp = basicData.value.get("host").map(_.str)
                    hostOp
                      .map { host =>
                        span(
                          trueV,
                          " (",
                          a(
                            target.blank,
                            href := s"https://www.ncbi.nlm.nih.gov/taxonomy/${trueV}",
                            host
                          ),
                          ")"
                        )
                      }
                      .getOrElse {
                        span(trueV)
                      }
                  }
                case "reference for biomaterial" =>
                  if (trueV.startsWith("http")) {
                    a(
                      target.blank,
                      href := s"${trueV}",
                      trueV
                    )
                  } else {
                    span(trueV)
                  }
                case _ => span(trueV)
              }
              tr(
                key := s"${header}",
                th(header),
                td(finalV)
              )
            }
          )
        )
      )
    )

  }

  def apply(id: String) = {
    Component(Props(id))
  }

}
